//
// Created by madison on 2022/8/1.
//

#include "string"

using namespace std;

class Solution {
public:
    // 方法一：筛选 + 判断
    bool isPalindrome(string s) {
        string sgood;
        for (char ch: s) {
            if (isalnum(ch)) {
                sgood += tolower(ch);
            }
        }
        string sgood_rev(sgood.rbegin(), sgood.rend());
        return sgood == sgood_rev;
    }

    bool isPalindrome1(string s) {
        int n = s.size();
        int left = 0, right = n - 1;
        while (left < right) {
            while (left < right && !isalnum(s[left])) {
                ++left;
            }
            while (left < right && !isalnum(s[right])) {
                --right;
            }
            if (left < right) {
                if (tolower(s[left]) != tolower(s[right])) {
                    return false;
                }
                ++left;
                --right;
            }
        }
        return true;
    }
};

int main() {
    Solution solution;
    printf("%d", solution.isPalindrome1("A man, a plan, a canal: Panama"));
    return 0;
}